from vtk import *
'''
    # FUNCTION TO FILTER EDGES IN THE LATTICE (NOT SURE OF IT YET)
    def recursiveConnect(d,e):
	print "rec"
	for f in parents[e]:
    if f in vertex:
    if f not in conns[d]:
    print f
    conns[d].append(f)
    #g.AddGraphEdge(vertex[d],vertex[f])
    else:
    recursiveConnect(d,f)
    '''

def write(root,extent,maxlev,ready):
	'''
        print ready
        if root in ready:
		return ""
        else:
		ready.append(root)
        '''
 	if extent[root]["level"] == maxlev:
 		return ""
	tab = ""
	#print root
	for i in range(extent[root]["level"]):
		tab += "\t"
	salida = tab+"<node size=\""+ str(len(extent[root]["objects"])) + "\" name=\""+ctr2(extent[root]["objects"][:5])+"\" >\n"
    
	for c in extent[root]["children"]:
		salida += write(c,extent,maxlev,ready)
	salida += tab+"</node>\n"
	return salida

def writeHierarchy(root,extent,maxlev,ready):
	'''
        print ready
        if root in ready:
		return ""
        else:
		ready.append(root)
        '''
 	if extent[root]["level"] == maxlev:
 		return ""
	tab = ""
	#print root
	for i in range(extent[root]["level"]):
		tab += "\t"
	salida = tab+"<node size=\""+ str(len(root.split(","))) + "\" name=\""+root+"\" >\n"
    
	for c in extent[root]["children"]:
		salida += writeHierarchy(c,extent,maxlev,ready)
	salida += tab+"</node>\n"
	return salida

def search(p,c,extent):
	if extent[p]["level"] == extent[c]["level"]:
		return False
	if c in extent[p]["children"]:
		return True
	ret = False
	for e in extent[p]["children"]:
		ret = ret and search(e,c,extent)
	return ret


def del_previous(c,e,extent):
	if extent[e]["level"] == 0:
		return False
	for p in extent[e]["parents"]:
		if p in extent[c]["parents"]:
			extent[c]["parents"].remove(p)
			extent[p]["children"].remove(c)
		del_previous(c,p,extent)

'''
    Clean the lattice concepts' objects recursively:
    Given two concepts X = (A,B), Y = (C,D) and X <= Y meaning that C is a subset of A 
    this algorithm cleans the intesection of C and A.
    '''
def cleanIntentBottomUp(e,forclean,besides,extent):
	if extent[e]["level"] <= 1:
		return ['']
	for p in extent[e]["parents"]:
		newfc = []
		for con in forclean:
			if con in extent[p]["objects"]:
				extent[p]["objects"].remove(con)
		for con in besides:
			if con in extent[p]["objects"]:
				extent[p]["objects"].remove(con)
				newfc.append(con)
		for con in extent[p]["objects"]:
			newfc.append(con)
		#arrow = str(extent[d]["level"])
		#for i in range(extent[d]["level"]-1):
		#	arrow += "-"
		#print arrow+">"+str(extent[p]["objects"][:5])
		cleanIntentBottomUp(p,forclean,newfc,extent)

'''
    Clean the lattice concepts' objects recursively:
    Given two concepts X = (A,B), Y = (C,D) and X <= Y meaning that C is a subset of A 
    this algorithm cleans the intesection of C and A.
    '''
def cleanExtentTopDown(e,forclean,besides,extent,maxlev):
	if extent[e]["level"] == maxlev:
		return ['']
	for p in extent[e]["children"]:
		newfc = []
		for con in forclean:
			if con in extent[p]["attributes"]:
				extent[p]["attributes"].remove(con)
		for con in besides:
			if con in extent[p]["attributes"]:
				extent[p]["attributes"].remove(con)
				newfc.append(con)
		for con in extent[p]["attributes"]:
			newfc.append(con)
		#arrow = str(extent[d]["level"])
		#for i in range(extent[d]["level"]-1):
		#	arrow += "-"
		#print arrow+">"+str(extent[p]["objects"][:5])
		cleanExtentTopDown(p,forclean,newfc,extent,maxlev)

'''
    Clean the lattice concepts' objects recursively:
    Given two concepts X = (A,B), Y = (C,D) and X <= Y meaning that C is a subset of A 
    this algorithm cleans the intesection of C andcleanExtentTopDown A.
    '''
def cleanIntentTopDown(e,forclean,besides,extent,maxlev):
	if extent[e]["level"] == maxlev:
		return ['']
	for p in extent[e]["children"]:
		newfc = []
		for con in forclean:
			if con in extent[p]["objects"]:
				extent[p]["objects"].remove(con)
		for con in besides:
			if con in extent[p]["objects"]:
				extent[p]["objects"].remove(con)
				newfc.append(con)
		for con in extent[p]["objects"]:
			newfc.append(con)
		#arrow = str(extent[d]["level"])
		#for i in range(extent[d]["level"]-1):
		#	arrow += "-"
		#print arrow+">"+str(extent[p]["objects"][:5])
		cleanIntentTopDown(p,forclean,newfc,extent,maxlev)

def getTopX(extent, X):
	stabs = {}
	for e in extent:
		d = str(extent[e]["stability"])+"-"+str(extent[e]["objects"])
		stabs[d] = e
	keys = stabs.keys()
	keys.sort(reverse=True)
	if len(keys) < X:
		limit = len(keys)
	else:
		limit = X
	intents = {}
	for i in range(limit):
		intent = extent[stabs[keys[i]]]["objects"]
		intent.sort()
		intents[str(intent)] = stabs[keys[i]]
	#print "\n\n\n*******************"
	#for i in intents:
	#	print i + str(extent[intents[i]]["stability"])
	return intents


# FORMATING FUNCTION (USELESS)
def ctr(ar):
	st = ""
	for i in ar:
		st+=i+"\n"
	return st

# FORMATING FUNCTION (USELESS)
def ctr2(ar):
	st = ""
	for i in ar:
		st+=i+", "
	return st


